//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.events.BlogSourceEvent;
	import reader.events.LabelTreeEvent;
	import reader.events.UserEvent;
	import reader.model.IDataProxy;
	import reader.model.vo.BlogFetchVO;
	import reader.view.components.LabelTree;

	public class LabelTreeMediator extends BaseApplicationMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		public static const NAME:String = "LabelTreeMediator";
		
		public function get labelTree():LabelTree
		{
			return this.viewComponent as LabelTree;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var dataProxy:IDataProxy;
		
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		
		public function LabelTreeMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			startUp();
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		public function startUp():void
		{
			dataProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.dataProxyName) as IDataProxy;
						
			labelTree.addEventListener(BlogSourceEvent.SOURCE_LABEL_CHANGE, onSourceLabelChange_handler, false, 0, true);
			labelTree.addEventListener(UserEvent.USER_IS_LABEL_NEW, onUserIsLabelNewChange_handler, false, 0, true);
			labelTree.addEventListener(LabelTreeEvent.LOAD_BLOGS, onStartLoadBlog_handler, false, 0, true);
			labelTree.addEventListener(LabelTree.MANAGE_SUNSCRIBE, onManageSubscribe_handler, false, 0, true);
			labelTree.addEventListener(LabelTree.REFRESH_LABELS, onRefreshLabels_handler, false, 0, true);
		}
		
		public function clearReference():void
		{
			dataProxy = null;
			
			labelTree.removeEventListener(BlogSourceEvent.SOURCE_LABEL_CHANGE, onSourceLabelChange_handler);
			labelTree.removeEventListener(UserEvent.USER_IS_LABEL_NEW, onUserIsLabelNewChange_handler);
			labelTree.removeEventListener(LabelTreeEvent.LOAD_BLOGS, onStartLoadBlog_handler);
			labelTree.removeEventListener(LabelTree.MANAGE_SUNSCRIBE, onManageSubscribe_handler);
			labelTree.removeEventListener(LabelTree.REFRESH_LABELS, onRefreshLabels_handler);
			
			facade.removeMediator(NAME);
		}
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.LABEL_TREE_CREATE,
					ApplicationFacade.CREATE_LABEL_TREE,
					ApplicationFacade.LOGOUT
					]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.CREATE_LABEL_TREE:
					dataProxy.getLabels();
					break;
				case ApplicationFacade.LABEL_TREE_CREATE:
					labelTree.labelTreeXML = notification.getBody() as XML;
					break;
				case ApplicationFacade.LOGOUT:
					clearReference();
					labelTree.labelTreeXML = null;
					break;
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//event
		
		private function onRefreshLabels_handler(e:Event):void
		{
			dataProxy.startAutoUpdateBlogItem();
		}
		
		private function onSourceLabelChange_handler(e:BlogSourceEvent):void
		{
			dataProxy.updateBlogSource(e.blogSource);
		}
		
		private function onUserIsLabelNewChange_handler(e:UserEvent):void
		{
			sendNotification(ApplicationFacade.USER_IS_LABEL_NEW, e.user);
			dataProxy.creatLabelTree();
		}
		
		private function onStartLoadBlog_handler(e:LabelTreeEvent):void
		{
			var bf:BlogFetchVO = new BlogFetchVO(e.blogSource, e.blogSourceIDArray);
			sendNotification(ApplicationFacade.BLOG_FETCH, bf);
		}
		
		private function onManageSubscribe_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.SETTING_OPEN);
		}
	}
}